<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i>库存管理</el-breadcrumb-item>
      <el-breadcrumb-item>入库管理</el-breadcrumb-item>
    </el-breadcrumb>

  <el-divider content-position="left"></el-divider>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-input v-model="key" placeholder="请输入内容">
          <el-button
              slot="append"
              @click="getList()"
              icon="el-icon-search">
          </el-button>
        </el-input>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-purple">
          房间类型:
          <template>
            <el-select v-model="status" @change="handleSelect()" placeholder="请选择">
              <el-option
                  v-for="item in options"
                  :key="item.status"
                  :label="item.label"
                  :value="item.status">
              </el-option>
            </el-select>
            <el-button type="primary" @click="range">刷新</el-button>
          </template>
        </div>
      </el-col>
    </el-row>
  </div>
  <el-divider content-position="left"></el-divider>
  <el-table :data="tableData" border style="width: 100%">
    <el-table-column prop="id" label="订单ID" width="80" align="center"></el-table-column>
    <el-table-column prop="productName" label="商品名称" header-align="center"
                     :show-overflow-tooltip="true"></el-table-column>
    <el-table-column prop="quantity" label="商品数量" width="100" align="center" header-align="center"
                     :show-overflow-tooltip="true"></el-table-column>
    <el-table-column prop="amount" label="订单金额" width="100" align="center" header-align="center"
                     :show-overflow-tooltip="true"></el-table-column>
    <el-table-column prop="status" label="商品状态" width="100" align="center" header-align="center"
                     :show-overflow-tooltip="true"></el-table-column>
    <el-table-column prop="notes" label="备注" width="100" align="center" header-align="center"
                     :show-overflow-tooltip="true"></el-table-column>
    <el-table-column prop="gmtCreate" label="创建时间" width="100" header-align="center"
                     :show-overflow-tooltip="true"></el-table-column>
    <el-table-column prop="gmtModified" label="操作时间" width="100" header-align="center"
                     :show-overflow-tooltip="true"></el-table-column>
    <el-table-column label="操作" width="250" align="center">
      <template slot-scope="scope">
        <el-button type="primary" plain class="op_button"  @click="confirm(scope.row)">确认入库</el-button>
        <el-button type="success" plain class="op_button"  @click="updateOrder()">新增订单</el-button>
      </template>
    </el-table-column>



  </el-table>
    <div class="block">
      <el-pagination
          @current-change="changePage"
          hide-on-single-page
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total,prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
  </div>

</template>

<script>
import axios from "axios";

export default {
  name: "SupplierManagement",
  data(){
    return{
      status:"",
      options:[{
        status: '待付款',
        label: '待付款'
      },{
        status: '待发货',
        label: '审核通过'
      },{
        status: '已发货',
        label: '已发货'
      },{
        status: '已完成',
        label: '已完成'
      },{
        status: '已关闭',
        label: '已关闭'
      },],
      key:"",
      tableData:[],
      currentPage: 1,
      pageSize: 0,
      total: 0,
    }
  },
  methods:{
    confirm(order){
      let url ='http://localhost:9902/order/updateOrder?id=' + order.id ;
      console.log('url = ' + url);
      axios.post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.loadCategoryList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });

    },
    // fetchData(){
    //   axios.get('http://localhost:9902/order/listStatus') //
    //       .then(response => {
    //         this.options = response.data.data;
    //       })
    //       .catch(error => {
    //         console.error(error);
    //       });
    // },
    handleSelect(){
      console.log("Selected value:", this.status);
      let page = this.$router.currentRoute.query.page;
      if (!page){
        page=1
      }

      axios.get('http://localhost:9902/order/listOrdersByStatus?status='+this.status+'&page='+page) //
          .then(response => {
            this.currentPage = response.data.data.currentPage;
            this.pageSize = response.data.data.pageSize;
            this.total = response.data.data.total;
            this.tableData = response.data.data.list;
          })
          .catch(error => {
            console.error(error);
          });
    },
    range(){
      this.status = "";
      this.loadOrderList();
    },
    getList(){
      let page = this.$router.currentRoute.query.page;
      if (!page){
        page=1
      }
      axios.get('http://localhost:9902/order/listOrdersByKey?key='+this.key+'&page='+page)
          .then(response => {
            let jsonResult =response.data;
            if (jsonResult.state == 20000){
              this.currentPage = jsonResult.data.pageNum;
              this.pageSize = jsonResult.data.pageSize;
              this.total = jsonResult.data.total;
              this.tableData = jsonResult.data.list;
            }else {
              this.$alert(jsonResult.message,'错误',{
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    updateOrder(){
      this.$router.push('/view/inventory/addCategory');
    },
    changePage(page) {
      this.$router.replace('?page=' + page);
      this.loadOrderList();
    },
    loadOrderList(){
      let page = this.$router.currentRoute.query.page;
      if (!page){
        page=1
      }
      let url = 'http://localhost:9902/order/allOrders?page='+page;
      axios.get(url)
          .then(response => {
            let jsonResult =response.data;
            if (jsonResult.state == 20000){
              this.currentPage = jsonResult.data.pageNum;
              this.pageSize = jsonResult.data.pageSize;
              this.total = jsonResult.data.total;
              this.tableData = jsonResult.data.list;
            }else {
              this.$alert(jsonResult.message,'错误',{
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
  },
  created() {
    let url = 'http://localhost:9902/order/allOrders/';
    this.axios.get(url).then((response)=>{
      this.tableData=response.data.data.list;
    })
  },
  mounted() {
    this.loadOrderList();
    // this.fetchData();
  }
}
</script>

<style scoped>

</style>